package thread.creadThread;

import com.ms.common.utils.ThreadUtils;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * 创建线程的方式
 *
 * @author ganbo
 * @date 2022/10/18 10:42
 */
public class CreadThreadDemo implements Runnable {
    @Override
    public void run() {
        ThreadUtils.sleepSeconds(2);
        System.out.println(Thread.currentThread().getName() + "=============子线程....");
    }


    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CreadThreadDemo d = new CreadThreadDemo();
        Thread t = new Thread(d);
        t.start();

        System.out.println("主线程==============");

        ExtentThread t1 = new ExtentThread();
        t1.start();

        CallableCreateThread c = new CallableCreateThread();
        FutureTask futureTask = new FutureTask(c);
        new Thread(futureTask).start();

        Object o = futureTask.get();
        System.out.println("总和为======>" + o);

        System.out.println("主线程结束了-----------------");


        new Thread(() -> System.out.println("hello world")).start();


    }
}

//继承Thread
class ExtentThread extends Thread {
    @Override
    public void run() {
        ThreadUtils.sleepSeconds(1);
        System.out.println(Thread.currentThread().getName() + " 通过继承实现线程创建");
    }
}

//实现callable接口
class CallableCreateThread implements Callable {
    @Override
    public Object call() throws Exception {
        int sum = 0;
        for (int i = 1; i <= 100; i++) {
            sum = sum + i;
        }

        System.out.println(Thread.currentThread().getName() + " ====callable接口实现实现线程=====");
        return sum;
    }
}
